package com.suray.wcs.service.websocket;

import com.suray.basic.wcs.path.enums.NodeType;
import com.suray.basic.wcs.utils.ParamConfig;
import com.suray.wcs.service.enums.WebSocketType;
import com.suray.wcs.service.pojo.NodeInfo;
import com.suray.wcs.service.system.entity.NodeDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:节点信息推送工具
 * @author: rongbj
 * @date:2019年11月13日 下午2:56:38
 */
@Component
public class NodeInfoPush {
    private static Logger log = LoggerFactory.getLogger(NodeInfoPush.class);

    /**
     * 节点信息推送方法
     *
     * @param nodeInfo 需要推送的节点
     */
    public static void push(NodeInfo nodeInfo) {
        List<NodeInfo> nodes = new ArrayList<>();
        if (nodeInfo.getGridType() != NodeType.B) {
            analyzeData(nodeInfo);
        }
        nodes.add(nodeInfo);
        try {
            WebSocketServer.sendInfo(nodes);
        } catch (Exception e) {
            log.error("点位信息推送遇到 错误！！！（建议刷新页面）");
            e.printStackTrace();
        }
    }

    private static void analyzeData(NodeInfo nodeInfo) {
        nodeInfo.setRunForGCodeStatus(analyzeRunForGCode(nodeInfo.getRunUpForGroundCodeStatus(),
                nodeInfo.getRunDownForGroundCodeStatus(), nodeInfo.getRunLeftForGroundCodeStatus(),
                nodeInfo.getRunRightForGroundCodeStatus(), nodeInfo.getCanToUp(), nodeInfo.getCanToDown(),
                nodeInfo.getCanToLeft(), nodeInfo.getCanToRight()));
        nodeInfo.setCalibrationTimeStatus(analyzeCalibrationTime(nodeInfo.getUpCalibrationTime(),
                nodeInfo.getDownCalibrationTime(), nodeInfo.getLeftCalibrationTime(),
                nodeInfo.getRightCalibrationTime(), nodeInfo.getCanToUp(), nodeInfo.getCanToDown(),
                nodeInfo.getCanToLeft(), nodeInfo.getCanToRight()));
    }

    private static int calcCalibrationTimeStatus(int calibrationTime) {
        if (calibrationTime == 0) {
            return 0;
        } else if (calibrationTime <= ParamConfig.maxCalibrationTime) {
            return 1;
        } else if (calibrationTime > ParamConfig.maxCalibrationTime) {
            return 2;
        }

        throw new RuntimeException("校准时间不能小于0：" + calibrationTime);
    }

    public static int analyzeCalibrationTime(Integer upTime, Integer downTime, Integer leftTime, Integer rightTime,
                                               boolean canUp, boolean canDown, boolean canLeft, boolean canRight) {
        int up, down, left, right;
        up = calcCalibrationTimeStatus(upTime);
        down = calcCalibrationTimeStatus(downTime);
        left = calcCalibrationTimeStatus(leftTime);
        right = calcCalibrationTimeStatus(rightTime);

        return analyzeResult(up, down, left, right, canUp, canDown, canLeft, canRight);
    }

    public static int analyzeRunForGCode(Integer upStatus, Integer downStatus, Integer leftStatus, Integer rightStatus,
                                          boolean canUp, boolean canDown, boolean canLeft, boolean canRight) {
        int up, down, left, right;
        up = upStatus == null ? 0 : upStatus;
        down = downStatus == null ? 0 : downStatus;
        left = leftStatus == null ? 0 : leftStatus;
        right = rightStatus == null ? 0 : rightStatus;

        return analyzeResult(up, down, left, right, canUp, canDown, canLeft, canRight);
    }

    private static int analyzeResult(int up, int down, int left, int right, boolean canUp, boolean canDown, boolean canLeft,
                              boolean canRight) {
        if (up == 0 && down == 0 && left == 0 && right == 0) {
            return 0;
        } else if (up == 2 || down == 2 || left == 2 || right == 2) {
            return 2;
        } else if ((!canUp || up == 1) && (!canDown || down == 1)
                && (!canLeft || left == 1) && (!canRight || right == 1)
        ) {
            return 1;
        } else {
            return 3;//未检测全
        }
    }
}
